import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class _49字母异位词分组 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String key = String.valueOf(charArray);
            //逻辑
            List<String> list = map.getOrDefault(key,new ArrayList<>());
            list.add(str);
            map.put(key,list);
        }
        List<List<String>> collect = new ArrayList<>(map.values());
        return collect;
    }

    public boolean isYiWei(String str1, String str2) {
        if (str1.length() != str2.length()) return false;
        //字母。多少次
        HashMap<Character, Integer> map1 = new HashMap<>();
        for (char c : str1.toCharArray()) {
            map1.put(c, map1.getOrDefault(c, 0) + 1);
        }
        HashMap<Character, Integer> map2 = new HashMap<>();
        for (char c : str2.toCharArray()) {
            map2.put(c, map2.getOrDefault(c, 0) + 1);
        }
        for (Map.Entry<Character, Integer> characterIntegerEntry : map2.entrySet()) {
            Character key = characterIntegerEntry.getKey();
            Integer value1 = characterIntegerEntry.getValue();
            Integer value2 = map1.get(key);
            if (!Objects.equals(value1, value2)) return false;
        }
        return true;

    }

    public static void main(String[] args) {
        String[] strs1 = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};

        _49字母异位词分组 man = new _49字母异位词分组();
//        System.out.println(man.isYiWei("eat", "tea"));

        List<List<String>> lists = man.groupAnagrams(strs1);

        for (List<String> list : lists) {
            System.out.println(list);
        }

        String[] strs2 = new String[]{};
        List<List<String>> lists2 = man.groupAnagrams(strs2);
        for (List<String> strings : lists2) {

            System.out.println(strings);
        }

        String[] strs3 = new String[]{"a"};
        List<List<String>> lists3 = man.groupAnagrams(strs3);
        for (List<String> strings : lists3) {
            System.out.println(strings);
        }


    }
}
